import argparse
import json
from easydict import EasyDict as edict

def init_config():
    parser = argparse.ArgumentParser()
    parser.add_argument('--config', type=str, default=None)
    parser.add_argument('--dataset', type=str, default=None)
    parser.add_argument('--network', type=str, default=None)
    parser.add_argument('--depth', type=int, default=None)
    parser.add_argument('--epoch', type=int, default=None)
    parser.add_argument('--learning_rate', type=float, default=None)

    parser.add_argument('--l1_regularization', type=float, default=0.0)
    parser.add_argument('--l2_regularization', type=float, default=0.0)

    parser.add_argument('--threshold', type=float, default=0.0)
    args = parser.parse_args()
    singal1 = 0
    singal2 = 0
    if args.config is not None:
        config = get_config_from_json(args.config)
    else:
        config = get_config_from_json('./configs/empty_config.json')
        singal1 = 1
    if args.dataset is not None:
        config.dataset = args.dataset
    else:
        singal2 = 1
    if args.network is not None:
        config.network = args.network
    else:
        singal2 = 1
    if args.depth is not None:
        config.depth = args.depth
    else:
        singal2 = 1
    if args.epoch is not None:
        config.epoch = args.epoch
    else:
        singal2 = 1
    if args.learning_rate is not None:
        config.learning_rate = args.learning_rate
    else:
        singal2 = 1
    if args.l1_regularization != 0:
        config.l1_regularization = args.l1_regularization
    if args.l2_regularization != 0:
        config.l2_regularization = args.l2_regularization
    if args.threshold != 0:
        config.threshold = args.threshold
    if singal1 == 1 and singal2 == 1:
        raise ValueError("基本所需参数无输入")
    return config


def get_config_from_json(jsonfile):
    with open(jsonfile, 'r') as config_file:
        config_dict = json.load(config_file)
    config = edict(config_dict)
    return config




